package com.itheima.reggie.exception;

import com.itheima.reggie.common.R;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
public class GlobalExceptionHandler {


    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R handlerException(SQLIntegrityConstraintViolationException exception){
        exception.printStackTrace();
        String message = exception.getMessage();//Duplicate entry '666' for key 'idx_username'
        String username = message.split(" ")[2].replace("'", "");
        return R.error("用户名"+username+"已存在");//

    }
}
